#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxh=10, maxn=10, maxm=10, maxk=9, inf=0x7ffffff;

int min(int a, int b) {
  return a<b ? a : b;
}

void initDis(int n, int m, int a[maxn+1][maxm+1], int dis[maxn+1][maxm+1][maxn+1][maxm+1]) {
  const int fx[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  memset(dis, 127, (maxn+1)*sizeof(*dis));
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      for (int k=0; k<4; k++) {
	int nx=i+fx[k][0], ny=i+fx[k][1];
	if (nx>0 && ny>0 && nx<=n && ny<=m) {
	  dis[i][j][nx][ny] = a[nx][ny];
	}
      }
    }
  }
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      for (int x=1; x<=n; x++) {
	for (int y=1; y<=m; y++) {
	  for (int nx=1; nx<=n; nx++) {
	    for (int ny=1; ny<=m; ny++) {
	      dis[x][y][nx][ny] = min(dis[x][y][nx][ny], dis[x][y][i][j]+dis[i][j][nx][ny]);
	    }
	  }
	}
      }
    }
  }
}

void initFromUp(int n, int m, int f[maxn+1][maxm+1][1<<maxk][maxn+1][maxm+1], int a[maxn+1][maxm+1], int tre[maxn+1][maxm+1], int dis[maxn+1][maxm+1][maxn+1][maxm+1], int ans[maxn+1][maxm+1]) {
  memset(f, 127, (maxn+1)*sizeof(*f));
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      for (int u=1; u<=n; u++) {
	for (int v=1; v<=m; v++) {
	  if (!tre[i][j]) {
	    f[i][j][0][u][v] = ans[i][j]+a[i][j]+dis[i][j][u][v];
	  } else {
	    f[i][j][1<<(tre[i][j]-1)][u][v] = ans[i][j]+a[i][j]+dis[i][j][u][v];
	  }
	}
      }
    }
  }
}

void dp(int n, int m, int f[maxn+1][maxm+1][1<<maxk][maxn+1][maxm+1], int a[maxn+1][maxm+1], int tre[maxn+1][maxm+1], int dis[maxn+1][maxm+1][maxn+1][maxm+1]) {
  for (int i=1)
}

int main() {
  freopen("treasure.in", "r", stdin);
  freopen("treasure.out", "w", stdout);

  static int f[maxn+1][maxm+1][1<<maxk][maxn+1][maxm+1], g[maxn+1][maxm+1][1<<maxk][maxn+1][maxm+1], ans[maxn+1][maxm+1], a[maxh+1][maxn+1][maxm+1], trea[maxh+1][maxn+1][maxm+1], k[maxh+1], dis[maxn+1][maxm+1][maxn+1][maxm+1];
  int h, n, m;
  scanf("%d %d %d", &h, &n, &m);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=n; j++) {
      for (int k=1; k<=n; k++) {
	scanf("%d", &a[i][j][k]);
      }
    }
  }
  for (int i=1; i<=h; i++) {
    scanf("%d", k+i);
    for (int j=1; j<=k[i]; j++) {
      int x, y;
      scanf("%d %d", &x, &y);
      if (trea[x][y]) {
	j--, k[i]--;
      } else {
	trea[i][x][y] = j;
      }
    }
  }

  memset(f, 127, sizeof(f));
  for (int i=1; i<=h; i++) {
    initDis(n, m, a[i], dis);
    initFromUp(n, m, f, a[i], trea[i], dis, ans);
    dp(n, m, f, a[i], trea[i], dis);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
